Ext.define('MyApp.controller.share.BaseGridController', {
	extend : 'Ext.app.Controller',
	init : function() {
		this.control({});
	},
	onGridSelect:function(view){
		var tabStore = Ext.myUtil.currentRevord.toolbarMenuEntities();
		tabStore.sort('seq', 'ASC');
		tabStore.data.each(function(item, index, len) {
			view.down('button#'+item.getData().itemId).setDisabled(false);
		});
	},
	onGridDSelect:function(view){
		var tabStore = Ext.myUtil.currentRevord.toolbarMenuEntities();
		tabStore.sort('seq', 'ASC');
		tabStore.data.each(function(item, index, len) {
			view.down('button#'+item.getData().itemId).setDisabled(true);
		});
	},
	onAddButtonClick : function(form) {
		Ext.widget(form, {animateTarget : document.body}).show();
	},
	onEditButtonClick : function(btn, view, form) {
		var record = btn.up(view).getSelectionModel().getLastSelected();
		var win = Ext.widget(form, {animateTarget : document.body});
		win.setTitle('修改记录');
		win.down('form').getForm().loadRecord(record);
		win.show();
	},
	onDeleteButtonClick : function(btn, viewName) {
		Ext.MessageBox.confirm('提示', '确定要删除此记录吗?', function showResult(
				btnResult) {
			if (btnResult == 'yes') {
				var view = btn.up(viewName);
				var record = view.getSelectionModel().getLastSelected();
				view.getStore().remove(record);
				view.getStore().sync({
					success : function(response) {
						var text = Ext
								.decode(response.operations[0].response.responseText);
						if (text == '0') {
							Ext.MessageBoxUtil.msg('提示', '操作失败!');
						} else {
							Ext.MessageBoxUtil.msg('提示', '操作成功!');
						}
					}
				});
			}
		});
	},
	onCancelButtonClick:function(btn){
		btn.up('window').close();
	},
	onSaveButtonClick : function(btn, store, model) {
		var win = btn.up('window');
		var form = win.down('form').getForm();
		if (form.isValid()) {
			var record = form.getRecord();
			if (record) {
				record.beginEdit();
				record.set(form.getValues());
				record.endEdit();
				store.sync({
					success : function(response) {
						var text = Ext
								.decode(response.operations[0].response.responseText);
						if (text == '0') {
							Ext.MessageBoxUtil.msg('提示', '数据提交失败!');
						} else {
							Ext.MessageBoxUtil.msg('提示', '数据提交成功!');
							win.close();
						}
					}
				});
			} else {
				record = Ext.create(model, form.getValues());
				store.add(record);
				store.sync({
					success : function(response) {
						var text = Ext
								.decode(response.operations[0].response.responseText);
						if (text == '0') {
							Ext.MessageBoxUtil.msg('提示', '数据提交失败!');
						} else {
							record.set('id', text.id);
							record.commit();
							Ext.MessageBoxUtil.msg('提示', '数据提交成功!');
							win.close();
						}
					}
				});
			}
		}
	}

});